//
//  HWHttpTool.m
//  黑马微博2期
//
//  Created by apple on 14-10-25.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "HWHttpTool.h"
#import "AFNetworking.h"

@implementation HWHttpTool

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    // 1.创建请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.requestSerializer.timeoutInterval = 10;
//    用 HTTPS 的方式访问未受信任的网站便会报错
    mgr.securityPolicy.validatesDomainName = NO;
    mgr.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
    //    mgr.requestSerializer=[AFJSONRequestSerializer serializer];//申明请求的数据是json类型
    [mgr.responseSerializer setAcceptableContentTypes:[[NSSet alloc] initWithObjects:@"text/html", @"text/json", @"text/javascript",@"application/json", nil]];
    // 2.发送请求
    
    [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    // 1.创建请求管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.requestSerializer.timeoutInterval = 10;
    if([url rangeOfString:@"trans/audioTrans"].location !=NSNotFound)//_roaldSearchText
    {
        mgr.requestSerializer.timeoutInterval = 30;
    }
    //用 HTTPS 的方式访问未受信任的网站便会报错
    mgr.securityPolicy.validatesDomainName = NO;
    mgr.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
    [mgr.responseSerializer setAcceptableContentTypes:[[NSSet alloc] initWithObjects:@"text/html", @"text/json", @"text/javascript",@"application/json", nil]];
//    NSData *cookiesdata = [[NSUserDefaults standardUserDefaults] objectForKey:@"kLoginCookie"];
//    
//    if([cookiesdata length]) {
//        NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookiesdata];
//        NSHTTPCookie *cookie;
//        for (cookie in cookies) {
//            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
//        }
//    }
//    
    //获取cookie
//    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
//    for (NSHTTPCookie *tempCookie in cookies) {
//        //打印获得的cookie
//        NSLog(@"getCookie: %@", tempCookie);
//    }
    // 2.发送请求
    [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

- (UIViewController*)topViewController
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    
    return [self topViewControllerWithRootViewController:window.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}


#pragma mark 2 上传数据
+ (void)postWithURL:(NSString *)urlStr
             params:(NSDictionary *)params
      formDataArray:(NSArray *)formDataArray
            success:(void (^)(id json))success
            failure:(void (^)(NSError *error))failure
{
    // 1  创建请求管理员
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.requestSerializer.timeoutInterval = 10;
    //    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    if([urlStr rangeOfString:@"trans/audioTrans"].location !=NSNotFound)//_roaldSearchText
    {
        mgr.requestSerializer.timeoutInterval = 30;
    }

    
    
    //用 HTTPS 的方式访问未受信任的网站便会报错
    mgr.securityPolicy.validatesDomainName = NO;
    mgr.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型
    [mgr.responseSerializer setAcceptableContentTypes:[[NSSet alloc] initWithObjects:@"text/html", @"text/json", @"text/javascript",@"application/json", nil]];
    // 2  发送POST请求
    [mgr POST:urlStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> totalFormData) {
        
        for (ZWFormData *formData in formDataArray)
        {
            [totalFormData appendPartWithFileData:formData.data name:formData.name fileName:formData.filename mimeType:formData.mimeType];
        }
    } progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (success)
        {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failure)
        {
            failure(error);
        }
        
    }];
    
}



+(void)AFNCancelRequest{
    [[[AFHTTPSessionManager manager]operationQueue] cancelAllOperations];
}







@end

#pragma mark - 三、实现上传数据模型

/**
 *  用来封装文件数据的模型
 */
@implementation ZWFormData

@end
